package com.jushu.storbox.newservice;

import java.util.ArrayList;
import java.util.List;

import com.jushu.storbox.constant.ClientConfig;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.MyShareListRequestV2;
import com.huawei.sharedrive.sdk.android.modelV2.request.Order;
import com.huawei.sharedrive.sdk.android.modelV2.request.ShareToMeRequestV2;
import com.huawei.sharedrive.sdk.android.modelV2.request.ThumbnailRequest;
import com.huawei.sharedrive.sdk.android.modelV2.response.ShareResourceV2;
import com.huawei.sharedrive.sdk.android.serviceV2.ShareClientV2;

public class RemoteShareService extends BaseAuthorizationService 
								implements IShareService {

	public final static int SHARE_PAGE_COUNT = 1000;
	
	private int modifyLimitCount(int limit) {
		if(limit <= 0) {
			limit = SHARE_PAGE_COUNT;
		}
		return limit;
	}
	
	@Override
	public ShareResourceV2 getSharedResourceByMe(int pageIndex, int limit) 
			throws ClientException {

		List<Order> orderList = new ArrayList<Order>();
		orderList.add(new Order(Order.ORDER_FILED_NAME, Order.ORDER_TYPE_ASC));
		
		List<ThumbnailRequest> thumbnailList = new ArrayList<ThumbnailRequest>();
		thumbnailList.add(new ThumbnailRequest(
				ClientConfig.THUMBNAIL_WIDTH, ClientConfig.THUMBNAIL_HEIGHT));
		
		limit = modifyLimitCount(limit);
		
		MyShareListRequestV2 request = new MyShareListRequestV2();
		request.setOffset(pageIndex * limit);
		request.setLimit(limit);
		request.setOrder(orderList);
		request.setThumbnail(thumbnailList);
		
		return ShareClientV2.getInstance().listShareResourceByMe(
				getAuthorization(), request);
	}
	
	@Override
	public ShareResourceV2 getSharedResourceToMe(int pageIndex, int limit, String keyWord) 
		throws ClientException {

		List<Order> orderList = new ArrayList<Order>();
		orderList.add(new Order(Order.ORDER_FILED_NAME, Order.ORDER_TYPE_ASC));
		
		List<ThumbnailRequest> thumbnailList = new ArrayList<ThumbnailRequest>();
		thumbnailList.add(new ThumbnailRequest(
				ClientConfig.THUMBNAIL_WIDTH, ClientConfig.THUMBNAIL_HEIGHT));
		
		limit = modifyLimitCount(limit);
		
		ShareToMeRequestV2 request = new ShareToMeRequestV2();
		request.setOffset(pageIndex * limit);
		request.setLimit(limit);
		request.setOrder(orderList);
		request.setThumbnail(thumbnailList);
		request.setKeyword(keyWord);
		
		return ShareClientV2.getInstance().listShareResourceToMe(
				getAuthorization(), request);
	}
}
